package cn.com.taiji.oospub.wechat.service.impl;

import cn.com.taiji.oospub.wechat.common.tool.TimeTools;
import cn.com.taiji.oospub.wechat.entity.CustomerOpinion;
import cn.com.taiji.oospub.wechat.entity.SchoolBaseInfo;
import cn.com.taiji.oospub.wechat.mapper.CustomerOpinionMapper;
import cn.com.taiji.oospub.wechat.mapper.SchoolBaseInfoMapper;
import cn.com.taiji.oospub.wechat.service.ICustomerOpinionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.write.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2020-10-28
 */
@Service
public class CustomerOpinionServiceImpl extends ServiceImpl<CustomerOpinionMapper, CustomerOpinion> implements ICustomerOpinionService {
    @Autowired
    private CustomerOpinionMapper customerOpinionMapper;
    @Autowired
    private SchoolBaseInfoMapper schoolBaseInfoMapper;

    @Override
    public void exportExcel(CustomerOpinion queryModel, HttpServletResponse response) {
        try {
            String fileName = ""; //文件名
            fileName = new String(("会员意见建议.xls").getBytes("gbk"),"iso8859-1");
            OutputStream os = response.getOutputStream();
            response.reset();
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setHeader("Content-disposition","attachment; filename="+fileName);

            //大标题字体(订单清单表.xls)
            WritableCellFormat wcfTitle = new WritableCellFormat();
            WritableFont BigTitleFont = new WritableFont(WritableFont.createFont("宋体"), 18, WritableFont.BOLD,false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
            wcfTitle.setAlignment(jxl.format.Alignment.CENTRE);
            wcfTitle.setVerticalAlignment(VerticalAlignment.CENTRE);
            wcfTitle.setFont(BigTitleFont);
            wcfTitle.setBorder(Border.ALL, BorderLineStyle.THIN);

            //表头字体
            WritableCellFormat wcfGrid = new WritableCellFormat();
            WritableFont gridTitleFont = new WritableFont(WritableFont.createFont("宋体"), 10, WritableFont.BOLD,false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
            wcfGrid.setAlignment(jxl.format.Alignment.CENTRE);
            wcfGrid.setVerticalAlignment(VerticalAlignment.CENTRE);
            wcfGrid.setFont(gridTitleFont);
            wcfGrid.setBorder(Border.ALL, BorderLineStyle.THIN);
            wcfGrid.setWrap(true);

            //内容字体
            WritableCellFormat wcfL = new WritableCellFormat();
            wcfL.setAlignment(jxl.format.Alignment.LEFT);
            wcfL.setVerticalAlignment(VerticalAlignment.CENTRE);
            wcfL.setBorder(Border.ALL, BorderLineStyle.THIN);
            wcfL.setWrap(true);

            WritableCellFormat wcfLNumber = new WritableCellFormat();
            wcfLNumber.setAlignment(jxl.format.Alignment.RIGHT);
            wcfLNumber.setVerticalAlignment(VerticalAlignment.CENTRE);
            wcfLNumber.setBorder(Border.ALL, BorderLineStyle.THIN);
            wcfLNumber.setWrap(true);

            WritableWorkbook wb = Workbook.createWorkbook(os);
            WritableSheet sheet = wb.createSheet("会员意见建议", 0);

            //大标题(订单清单表.xls)
            sheet.mergeCells(0, 0, 4, 0);
            sheet.setRowView(0, 1000);
            sheet.addCell(new Label(0, 0, "会员意见建议",wcfTitle));

            //表头
            sheet.addCell(new Label(0, 1, "项目中文名",wcfGrid));
            sheet.setColumnView(0, 14);
            sheet.addCell(new Label(1, 1, "项目英文名",wcfGrid));
            sheet.setColumnView(1, 14);
            sheet.addCell(new Label(2, 1, "建议人",wcfGrid));
            sheet.setColumnView(2, 20);
            sheet.addCell(new Label(3, 1, "建议内容",wcfGrid));
            sheet.setColumnView(3, 15);
            sheet.addCell(new Label(4, 1, "建议时间",wcfGrid));
            sheet.setColumnView(4, 10);
            //根据条件查找
            List<CustomerOpinion> list = this.customerOpinionMapper.listQuery(queryModel);
            int row = 2;
            int col = 0;
            if(null != list && list.size()>0){
                //菜品细目
                Double sumAmount=0.00d;
//    			Double sumAmountLogistic=0.00d;
                for(int i=0;i<list.size();i++){
                    CustomerOpinion customerOpinion = list.get(i);
                    SchoolBaseInfo school = schoolBaseInfoMapper.selectById(customerOpinion.getSchoolId());
                    sheet.addCell(new Label(col++, row, school.getNameZh(),wcfL));//所属项目中文
                    sheet.addCell(new Label(col++, row, school.getNameEn(),wcfL));//所属项目英文
                    sheet.addCell(new Label(col++, row, customerOpinion.getCreator().getName(),wcfL));//订单编号
                    sheet.addCell(new Label(col++, row, customerOpinion.getContent(),wcfLNumber));//总计
                    sheet.addCell(new Label(col++, row, TimeTools.calendar2String(customerOpinion.getCreateTime(), TimeTools.DAY_FORMAT),wcfL));//状态
                    row ++;
                    col = 0;
                }
            }
            wb.write();
            wb.close();
            os.flush();
            os.close();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (WriteException e) {
            e.printStackTrace();
        }
    }
}
